package com.jp.knowledge.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.jp.knowledge.R;
import com.jp.knowledge.util.L;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiayen on 16/9/27.
 * 顶部横向滑动view
 */
public class TopItemView extends HorizontalScrollView implements View.OnClickListener {

    private Context mContext;
    private HorizontalScrollView   contentView;

    private LinearLayout itemLayout;

    private int selectedPostion=0;//默认选中的位置
    private int curPostion=0;//当前位置
    private int lastPostion = 0;//上一个位置
    private ItemChangeListerner itemchange;
    private List<ItemView> itemViews;
    public TopItemView(Context context) {
        this(context,null);
    }

    public TopItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TopItemView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
        this.mContext=context;
        init();
    }

    public void setOnItemChangeListener(ItemChangeListerner itemchange){
        this.itemchange = itemchange;
    }

    private void init() {
        contentView = (HorizontalScrollView) LayoutInflater.from(mContext).inflate(R.layout.top_item_main,null);
        addView(contentView);
        itemLayout = (LinearLayout) contentView.findViewById(R.id.items);
    }

    private void addItem(String name,int postion){
        ItemView itemView = new ItemView(mContext);
        itemView.setPostion(postion);
        itemView.setName(name);
//        itemViews[postion]=itemView;
        itemViews.add(postion,itemView);
        if(selectedPostion==postion){
            itemView.setNameColor(R.color.theme_color);
        }
        itemLayout.addView(itemView);
        itemView.setOnClickListener(this);

    }

    /**
     * 更新
     * @param names
     */
    public void  updateItem(String[] names){
//        selectedPostion=0;//默认选中的位置
//        curPostion=0;
//        selectedCur(curPostion);
        selectedPostion =curPostion;
        itemLayout.removeAllViews();
        addItems(names);

    }
    public void addItems(String[] names){

        int size = names.length;
        itemViews = new ArrayList<>();
        for (int i = 0; i <size ; i++) {
            addItem(names[i],i);
        }
    }

    public void  changeItemViews(int fromPosition,int toPosition){

        ItemView itemView =  itemViews.get(fromPosition);
        itemViews.remove(fromPosition);
        itemViews.add(toPosition,itemView);

//        Collections.swap(itemViews,fromPosition,toPosition);
    }

    /**
     * 获取宽带
     * @return
     */
    public int getItemWidth(int position){
        return itemViews.get(position).getWidth();
    }

    /**
     * 获取item在当前屏幕的位置
     * @param position
     * @return
     */
    public int[] getItemPositionOnWindow(int position){
        int[] location = new int[2];
        itemViews.get(position).getLocationInWindow(location);
        return location;
    }

    @Override
    public void onClick(View view) {
            ItemView itemView = (ItemView) view;
            int position = itemView.getPostion();
//            selectedCur(position);
            if(itemchange!=null){
                itemchange.done(itemView,position);
            }
    }

    public void selectedCur(int position){
        if(position!=curPostion) {//过滤重复点击
            ItemView itemView = itemViews.get(position);
            itemView.setNameColor(R.color.theme_color);
            itemViews.get(lastPostion).setNameColor(R.color.font_black_think);
            curPostion = position;
            lastPostion = curPostion;
        }
    }

    /**
     * 清掉选中
     */
    public void clearDefault(){
        itemViews.get(0).setNameColor(R.color.font_black_think);
        L.e("clearDefault========"+ itemViews.get(0).getName());
    }

    /**
     * 设置默认选中状态
     */
    public void setDefult(){
        itemViews.get(0).setNameColor(R.color.theme_color);
        L.e("setDefult========"+ itemViews.get(0).getName());
        scrollTo(0,0);
    }

    public int getCurPostion() {
        return curPostion;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }

    public LinearLayout  getContentView(){
        return itemLayout;
    }

    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
    }

    public interface  ItemChangeListerner{
        void done(ItemView itemView,int position);
    }
}
